package org.jiuling.aliyun.sms.convert;

import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.model.QuerySendDetailsResponseModel;
import org.jiuling.aliyun.sms.model.enums.SendStatusEnum;
import org.jiuling.basics.exception.BasicsException;
import org.jiuling.basics.util.EnumUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询发送给详情响应转换
 * @author 紫灵
 * @date 2023年02月10日 14时14分52秒
 */
public class QuerySendDetailsResponseConvert {

    /** 私有构造方法，禁止创建对象 */
    private QuerySendDetailsResponseConvert() {
    }

    /**
     * 内容转模型
     * @param querySendDetailsResponseBody 查询发送详情请求内容
     * @return 查询发送详情请求模型
     */
    public static QuerySendDetailsResponseModel bodyToModel(com.aliyun.sdk.service.dysmsapi20170525.models.QuerySendDetailsResponseBody querySendDetailsResponseBody){
        QuerySendDetailsResponseModel querySendDetailsResponseModel = new QuerySendDetailsResponseModel();
        querySendDetailsResponseModel.setCode(querySendDetailsResponseBody.getCode());
        querySendDetailsResponseModel.setMessage(querySendDetailsResponseBody.getMessage());
        querySendDetailsResponseModel.setRequestId(querySendDetailsResponseBody.getRequestId());
        querySendDetailsResponseModel.setTotalCount(querySendDetailsResponseBody.getTotalCount());

        List<QuerySendDetailsResponseModel.SmsSendDetailModel> smsSendDetailModelList = querySendDetailsResponseBody.getSmsSendDetailDTOs().getSmsSendDetailDTO().stream().map(smsSendDetailDTO -> {
            QuerySendDetailsResponseModel.SmsSendDetailModel smsSendDetailModel = new QuerySendDetailsResponseModel.SmsSendDetailModel();
            smsSendDetailModel.setErrCode(smsSendDetailDTO.getErrCode());
            smsSendDetailModel.setTemplateCode(smsSendDetailDTO.getTemplateCode());
            smsSendDetailModel.setOutId(smsSendDetailDTO.getOutId());
            smsSendDetailModel.setReceiveDate(smsSendDetailDTO.getReceiveDate());
            smsSendDetailModel.setSendDate(smsSendDetailDTO.getSendDate());
            smsSendDetailModel.setPhoneNum(smsSendDetailDTO.getPhoneNum());
            smsSendDetailModel.setContent(smsSendDetailDTO.getContent());
            try {
                smsSendDetailModel.setSendStatus(EnumUtil.valueOfEnum(SendStatusEnum.class, smsSendDetailDTO.getSendStatus(), "getCode"));
            } catch (BasicsException e) {
                throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
            }
            return smsSendDetailModel;
        }).collect(Collectors.toList());

        querySendDetailsResponseModel.setSmsSendDetailList(smsSendDetailModelList);
        return querySendDetailsResponseModel;
    }

    /**
     * 内容转模型
     * @param querySendDetailsResponseBody 查询发送详情请求内容
     * @return 查询发送详情请求模型
     */
    public static QuerySendDetailsResponseModel bodyToModel(com.aliyun.dysmsapi20170525.models.QuerySendDetailsResponseBody querySendDetailsResponseBody){
        QuerySendDetailsResponseModel querySendDetailsResponseModel = new QuerySendDetailsResponseModel();
        querySendDetailsResponseModel.setCode(querySendDetailsResponseBody.getCode());
        querySendDetailsResponseModel.setMessage(querySendDetailsResponseBody.getMessage());
        querySendDetailsResponseModel.setRequestId(querySendDetailsResponseBody.getRequestId());
        querySendDetailsResponseModel.setTotalCount(querySendDetailsResponseBody.getTotalCount());

        List<QuerySendDetailsResponseModel.SmsSendDetailModel> smsSendDetailModelList = querySendDetailsResponseBody.getSmsSendDetailDTOs().getSmsSendDetailDTO().stream().map(smsSendDetailDTO -> {
            QuerySendDetailsResponseModel.SmsSendDetailModel smsSendDetailModel = new QuerySendDetailsResponseModel.SmsSendDetailModel();
            smsSendDetailModel.setErrCode(smsSendDetailDTO.getErrCode());
            smsSendDetailModel.setTemplateCode(smsSendDetailDTO.getTemplateCode());
            smsSendDetailModel.setOutId(smsSendDetailDTO.getOutId());
            smsSendDetailModel.setReceiveDate(smsSendDetailDTO.getReceiveDate());
            smsSendDetailModel.setSendDate(smsSendDetailDTO.getSendDate());
            smsSendDetailModel.setPhoneNum(smsSendDetailDTO.getPhoneNum());
            smsSendDetailModel.setContent(smsSendDetailDTO.getContent());
            try {
                smsSendDetailModel.setSendStatus(EnumUtil.valueOfEnum(SendStatusEnum.class, smsSendDetailDTO.getSendStatus(), "getCode"));
            }catch (BasicsException e) {
                throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
            }
            return smsSendDetailModel;
        }).collect(Collectors.toList());

        querySendDetailsResponseModel.setSmsSendDetailList(smsSendDetailModelList);
        return querySendDetailsResponseModel;
    }
}
